﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Samples.WindowsAzure.ServiceManagement;
using System.Security.Cryptography.X509Certificates;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WAPA.Framework
{
    /// <summary>
    /// Holds config settings and values used for making API calls.
    /// </summary>
    public static class ManagementContext
    {
        public const string RegistrationQueueName = "registration";
        public const string TaskQueueName = "task";
        public const string ReportQueueName = "report";
        public const string NotificationQueueName = "notification";

        public static string SubscriptionId;
        public static int AccountQuotaHostedServices;
        public static int AccountQuotaCores;
        public static int AccountQuotaStorage;
        public static string CertificateThumbprint;
        public static X509Certificate2 certificate;
        public static string StorageAccountName;
        public static string QueueEndpointUri { get { return "http://" + StorageAccountName + ".queue.core.windows.net" + "/"; } }
        public static string BlobEndpointUri { get { return "http://" + StorageAccountName + ".blob.core.windows.net" + "/"; } }
        public static string TableEndpointUri { get { return "http://" + StorageAccountName + ".table.core.windows.net" + "/"; } }
        public static string AccessKey;
        public static string WorkerConfig;
        public static string WorkerConfigFile;
        public static string CertificatePrivateKeyPassword;
        public static List<OperationContext> Operations = new List<OperationContext>();
        public static bool LockOperationList = false;
        public static string CurrentActionDescription;
        public static string PackageContainerName;
        public static string PackageBlobName;
        public static string PackageFileName;
        //Default to 
        public static string PackageFileFolder;
        public static string OutputContainerName;
        public static string ExecutionAssemblyLocation;
        public static string PiCalculatorBinariesLocation;

        public static LocationList Locations;

        public static HostedServiceList HostedServices;
        public static DateTime HostedServicesLastRefresh;

        public static void LockOperations()
        {
            ManagementContext.LockOperationList = true;
        }

        public static void UnLockOperations()
        {
            ManagementContext.LockOperationList = false;
        }

        
        public static bool Online
        {
            get
            {
                try
                {
                    System.Net.IPHostEntry entry = System.Net.Dns.GetHostEntry("www.microsoft.com");
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public static Scenario ActiveScenario;

        public delegate void OperationAddedHandler(OperationContext operationContext);
        public static event OperationAddedHandler OperationAdded;

        public delegate void OperationUpdatedHandler(OperationContext operationContext);
        public static event OperationUpdatedHandler OperationUpdated;


        public static void AddOperationContext(OperationContext operationContext)
        {
            while (ManagementContext.LockOperationList)
            {
                
            }



            Operations.Add(operationContext);

            if (OperationAdded != null)
                OperationAdded(operationContext);
        }

        public static void UpdateOperationContext(OperationContext operationContext)
        {
            OperationContext originalOperationContext = ManagementContext.Operations.Where(o => o.OperationId == operationContext.OperationId).First();

            originalOperationContext.OperationDescription = operationContext.OperationDescription;
            originalOperationContext.CommunicationExceptionMessage = operationContext.CommunicationExceptionMessage;
            originalOperationContext.Completed = operationContext.Completed;
            originalOperationContext.ErrorMessage = operationContext.ErrorMessage;
            originalOperationContext.HttpStatusCode = operationContext.HttpStatusCode;
            originalOperationContext.Status = operationContext.Status;
            originalOperationContext.StatusDescription = operationContext.StatusDescription;
            originalOperationContext.TrackingId = operationContext.TrackingId;

            if (OperationUpdated != null)
                OperationUpdated(originalOperationContext);
        }
    }
}
